<?php

/**
 * @Author: kendy
 * @Date:   2019-10-23 17:03:13
 * @Last Modified by:   jiayanting

 */

namespace app\common\model;

use think\Model;
use think\Db;

class AppraiseModel extends Model
{
    /**
     * 获取不同类型产品的评价信息
     * @Author   kendy
     * @DateTime 2019-10-23T17:09:50+0800
     * @param    [item_id] 评价的对象id
     * @param    [type] 类型 1景点，2酒店，3跟团，4导游
     * @param    [page]  当前页数
     * @param    [limit] 每页显示个数
     * @return   [type]
     */
    public function getAppList($type, $item_id, $page, $limit = '', $eval_status = '', $image = false)
    {
        if ($eval_status == 1 || $eval_status == 2) {
            $where['eval_status'] = (int)$eval_status;
        }
        if ($image) {
            $where["a.images"] = array('NEQ', '');
        }
        //type 1酒店，2景点，3跟团，4导游
        $where['a.is_display'] = 0;
        $where['a.item_id'] = $item_id;
        $where['a.type'] = $type;
        $limit = ($limit == '') ? config('page_size') : $limit;
        $idName = 'id';
        if ($type == 2) {
            $table = 'hotel';
            $idName = 'hotel_id';
        } elseif ($type == 1) {
            $table = 'tours';
            $idName = 'tour_id';
        } elseif ($type == 3) {
            $table = 'group_tour';
        } else {
            $table = 'guide';
            $idName = 'guide_id';
        }
        $resultData = Db::name($table)->alias('product')
            ->field('a.item_id,app_id,a.type,a.content,a.images,order_id,a.create_time,u.user_icon,u.nickname,eval_status,star')
            ->join('appraise a', 'a.item_id = product.' . $idName . '', 'inner')
            ->join('user u', 'u.uid = a.user_id', 'inner')
            ->where($where)
            ->order('a.app_id desc')
            ->limit(($page - 1) * $limit, $limit)
            ->select();
        if (count((array)$resultData) > 0) {
            foreach ($resultData as $key => $value) {
                $resultData[$key]['images'] = [];
                if (!empty($value['images'])) 
                    $resultData[$key]['images'] = explode(',', $value['images']);
                if (count($resultData[$key]['images']) > 0) {
                    foreach ($resultData[$key]['images'] as $keys => $values) {
                        $resultData[$key]['images'][] = addWaterByField($values);
                    }
                  $resultData[$key]['images'] = array_slice(explode(',', $value['images']), 0, 3);
                }

               

                $resultData[$key]['create_time'] = date('Y-m-d', $value['create_time']);
            }
        }

        return $resultData;

    }

    /**
     * [getAppCountByItemId 获取评价的个数]
     * @Author   kendy
     * @DateTime 2019-10-28T14:59:43+0800
     * @param    [type]                   $type    [评价类型 1景点，2酒店，3跟团，4导游]
     * @param    [type]                   $item_id [对象id]
     * @return   [type]                            [description]
     */
    public function getAppCountByItemId($type, $item_id, $eval_status = '', $image = false)
    {
        if ($eval_status == 1 || $eval_status == 2) {
            $where['eval_status'] = (int)$eval_status;
        }
        if ($image) {
            $where["images"] = array('NEQ', '');
        }
        $where['is_display'] = 0;
        $where['item_id'] = $item_id;
        $where['type'] = $type;
        $resultData = Db::name('appraise')
            ->where($where)
            ->count();
        return $resultData;
    }

    /**
     * [getAppRate 获取对象好评率]
     * @Author   kendy
     * @DateTime 2019-10-28T16:50:26+0800
     * @param    [type]                   $type    [评价类型 1景点，2酒店，3跟团，4导游]
     * @param    [type]                   $item_id [对象id]
     * @return   [type]                            [description]
     */
    public function getAppRate($type, $item_id)
    {
        $app_all = $this->getAppCountByItemId($type, $item_id);
        $app_good = Db::name('appraise')
            ->where('type', $type)
            ->where('item_id', $item_id)
            ->where('is_display', 0)
            ->where('eval_status', 1)
            ->count();
        $app_cha = Db::name('appraise')
            ->where('type', $type)
            ->where('item_id', $item_id)
            ->where('is_display', 0)
            ->where('eval_status', 0)
            ->count();
        if (empty($app_all)) {
            return 100;
        }
        if(empty($app_good) && !empty($app_cha)){
            return 0;
        }else {
            return round($app_good / $app_all, 2) * 100;
        }

    }

    /**
     * [delApp 删除评论]
     * @Author   kendy
     * @DateTime 2019-12-25T09:59:05+0800
     * @param    [type]                   $app_id [评论id]
     * @return   [type]                           [description]
     */
    public function delApp($app_id)
    {
        $res = Db::name('appraise')
            ->where('app_id', $app_id)
            ->find();
        if (!$res) {
            return true;
        } else {
            $info = Db::name('appraise')
                ->where('app_id', $app_id)
                ->update(['is_display' => 1]);
            if ($info)
                return true;
        }
        return false;
    }


}
